//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.mxpioframework.security.access.filter;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.mxpioframework.cache.provider.CacheProvider;
import com.mxpioframework.common.util.SpringUtil;
import com.mxpioframework.security.anthentication.JwtLoginToken;
import com.mxpioframework.security.entity.User;
import com.mxpioframework.security.service.OnlineUserService;
import com.mxpioframework.security.util.TokenUtil;
import java.io.IOException;
import java.util.Date;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.web.filter.OncePerRequestFilter;

public class JwtTokenFilter extends OncePerRequestFilter {
    private static final Logger log = LoggerFactory.getLogger(JwtTokenFilter.class);
    private RequestMatcher requiresAuthenticationRequestMatcher = new RequestHeaderRequestMatcher("Access-Token");

    public JwtTokenFilter() {
    }

    protected String getJwtToken(HttpServletRequest request) {
        String authInfo = request.getHeader("Access-Token");
        return authInfo;
    }

    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        if (!this.requiresAuthentication(httpServletRequest, httpServletResponse)) {
            filterChain.doFilter(httpServletRequest, httpServletResponse);
        } else {
            try {
                String token = this.getJwtToken(httpServletRequest);
                if (StringUtils.isEmpty(token)) {
                    filterChain.doFilter(httpServletRequest, httpServletResponse);
                    return;
                }

                CacheProvider cacheProvider = (CacheProvider)SpringUtil.getBean(CacheProvider.class);
                OnlineUserService onlineUserService = (OnlineUserService)SpringUtil.getBean(OnlineUserService.class);
                if (cacheProvider != null) {
                    User user = onlineUserService.getOne(token, cacheProvider);
                    if (user == null) {
                        filterChain.doFilter(httpServletRequest, httpServletResponse);
                        return;
                    }

                    JwtLoginToken jwtLoginToken = new JwtLoginToken(user, "", user.getAuthorities());
                    jwtLoginToken.setDetails(new WebAuthenticationDetails(httpServletRequest));
                    SecurityContextHolder.getContext().setAuthentication(jwtLoginToken);
                    filterChain.doFilter(httpServletRequest, httpServletResponse);
                } else {
                    DecodedJWT jwt = TokenUtil.verifyToken(token);
                    Date date = jwt.getExpiresAt();
                    if (date.before(new Date())) {
                        filterChain.doFilter(httpServletRequest, httpServletResponse);
                        return;
                    }

                    User user = (User)JSON.parseObject(jwt.getSubject(), User.class);
                    JwtLoginToken jwtLoginToken = new JwtLoginToken(user, "", user.getAuthorities());
                    jwtLoginToken.setDetails(new WebAuthenticationDetails(httpServletRequest));
                    SecurityContextHolder.getContext().setAuthentication(jwtLoginToken);
                    filterChain.doFilter(httpServletRequest, httpServletResponse);
                }
            } catch (Exception var11) {
                log.info(httpServletRequest.getRequestURI());
                log.error("错误信息：" + var11.getMessage());
                //var11.printStackTrace();
            }

        }
    }

    protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) {
        return this.requiresAuthenticationRequestMatcher.matches(request);
    }
}
